import express from 'express'
import { createApp } from './app.js'
import { renderToString } from 'react-dom/server'

const server = express()

server.get('/', (req, res) => {
    const app = createApp()

    const html = renderToString(app)

    res.send(`
        <!DOCTYPE html>
        <html>
                <head>
                <title>React SSR Example</title>
                </head>
                <body>
                <div id="app">${html}</div>
                <script type="importmap">
                {
                    "imports": {
                        "react": "https://esm.sh/react@18.2.0",
                        "react-dom/client": "https://esm.sh/react-dom@18.2.0/client"
                    }
                }
                </script>
                <script type="module" src="/client.js"></script>
                </body>
        </html>
    `)
})

server.use(express.static('.'))

server.listen(3000, () => {
    console.log('ready', process.cwd())
})
